//查看返回
var clue_source_links;

function check_cancel() {
	clue_source_links = "";
	$('#view_call').removeAttr('disabled');
	$('.view_pop').css('visibility', 'hidden');
	$('#view_form').css('transform', 'scale(0)');
	let ue = UE.getEditor('editorchak');
	ue.ready(function() {
		ue.setContent(""); //设置文本编辑器
	});
	$('#view_form input').val("")
	$('#LAY_table_roled_son').text("")
	$('body,html').removeClass('bh_hide');
	$('#effective_statu').find('option').remove().end();
	$('#effective_status').find('option').remove().end();
	$('#clue_create_time,#clue_create_name,#clue_owner_name,#clue_update_time,#clue_project,#clue_phone,#clue_area,#clue_wechat,#clue_qq,#clue_number,#clue_unit,#clue_platform,#clue_client,#clue_account_name,#clue_coding,#clue_source_platform,#clue_category,#clue_ip,#clue_source,#clue_source_link,#clue_key,#clue_details,#owner_fixed,#effective_fixed').text("");

};

$('.return_bth').click(function() {
	check_cancel();
});
Location_();
var user_name = getCookie("user_name");
$('.user_name').text(user_name);
$('#owner_role_idtj').val(role_id);

function edit_empty() {
	$('#effective_statu3').find('option').remove().end();
	let ue = UE.getEditor('editorbianj');
	ue.ready(function() {
		ue.setContent(""); //设置文本编辑器
	});
	$('.editor-content input').val("")
};

//编辑客户返回按键
function edit_cancel() {
	$('.call_phone_btn').removeAttr('disabled');
	$('.Editor-page').css('visibility', 'hidden');
	$('.editor_con').css('transform', 'scale(0)');
	edit_empty();
};

$('.editor-title button').click(function() {
	edit_cancel();
});

$(document).keydown(function(e) {
	if(e.altKey && e.keyCode == 88) {　
		edit_cancel();
		check_cancel();
	}
});
//点击添加客户返回按钮
$('.return_add_clues').click(function() {
	$('.add_user').css('visibility', 'hidden');
	$('.add_user .content').css('transform', 'scale(0)');
	$('.form_content input').val("");
});

$('#dele_').click(function() {
	$('#view_call').removeAttr('disabled');
	$('.view_pop').css('visibility', 'hidden');
	$('#view_form').css('transform', 'scale(0)');
	let ue = UE.getEditor('editorchak');
	ue.ready(function() {
		ue.setContent(""); //设置文本编辑器
	});
	$('#view_form input').val("")
	$('#LAY_table_roled_son').text("")
	$('body,html').removeClass('bh_hide');
	$('#effective_statu').find('option').remove().end();
	$('#effective_status').find('option').remove().end();
	$('#clue_create_time,#clue_create_name,#clue_owner_name,#clue_update_time,#clue_project,#clue_phone,#clue_area,#clue_wechat,#clue_qq,#clue_number,#clue_unit,#clue_platform,#clue_client,#clue_account_name,#clue_coding,#clue_source_platform,#clue_category,#clue_ip,#clue_source,#clue_source_link,#clue_key,#clue_details').text("");

});

//点击添加客户
$('.add_user2').click(function() {
	$('#owner_name1').val(user_name);
	//	$('.add_user').css('display', 'block');
	$('.add_user').css('visibility', 'visible');
	$('.add_user .content').css('transform', 'scale(1)');
});

//点击选择客户负责人Cancel按钮
$('.Cancel').click(function() {
	$('.Trace_leader').css('visibility', 'hidden');
	$('.Trace_leader .content').css('transform', 'scale(0)');
});
//点击admin框
$('#owner_name1').click(function() {
	$('.Trace_leader').css('visibility', 'visible');
	$('.Trace_leader .content').css('transform', 'scale(1)');
});
$('#owner_name2').click(function() {
	$('.Trace_leader').css('visibility', 'visible');
	$('.Trace_leader .content').css('transform', 'scale(1)');
});
$('#owner_name').click(function() {
	$('.Trace_leader').css('visibility', 'visible');
	$('.Trace_leader .content').css('transform', 'scale(1)');
});
//点击线索分配cancel按钮
$('.cancel').click(function() {
	$('.Allcation').css('visibility', 'hidden');
	$('.Allcation .content').css('transform', 'scale(0)');
	$('#owner_name').val("");
})

//点击admin框
$('#owner_nameadmin').click(function() {
	$('.Trace_leader').css('visibility', 'visible');
	$('.Trace_leader .content').css('transform', 'scale(1)');
})

var departent_name;
var role_id;
//网谈table
layui.use(['table', 'form', 'jquery', 'layer', 'rate', 'laydate', 'element'], function() {
	var $ = layui.$ //重点处
		,
		layer = layui.layer;
	var form = layui.form;
	var table = layui.table;
	var element = layui.element;
	var rate = layui.rate;
	var laydate = layui.laydate;

	//创建日期范围
	laydate.render({
		elem: '#test6',
		range: true
	});

	//属性下拉框
	var property_seach_option = '<option value="8">--请选择--</option>' +
		'<option value="0">网谈</option>' +
		'<option value="1">留言</option>' +
		'<option value="2">热线</option>';

	var getPicTabNum = localStorage.getItem("picTabNum"); //获取缓存
	var allcation_id = [];

	//下载资源
	var down_url = "http://" + com_url + "//admin/statistics?role_id=" + role_id;
	$('#down_url').on('click', function() {
		window.location.href = down_url;
	});

	var down_url_tomoro = "http://" + com_url + "//admin/yesterdayStatistics?role_id=" + role_id;
	$('#down_url_tomoro').on('click', function() {
		window.location.href = down_url_tomoro;
	});

	var down_url_user = "http://" + com_url + "//admin/derivedData?role_id=" + role_id;
	$('#down_url_user').on('click', function() {
		window.location.href = down_url_user;
	});

	//监听表格复选框选择
	table.on('checkbox(demo)', function(obj) {});
	var principal;
	var targetss = $('#test4');
	var floor;
	var zxh_mobile;
	var url_table = "http://" + com_url + "//admin/client";
	var clientId = [];
	var tip_index = 0;

	//	$('.layui-layer-tips').on('mouseenter', function() {
	//		tip_index = layer.tips('我出来啦~| ू•ૅω•́)ᵎᵎᵎ', {
	//			time: 0
	//		});
	//	});
	//	$(document).on('mouseenter', '.laytable-cell-2-cot', function() {
	//		tip_index = layer.tips('我出来啦ᵎ我出来啦ᵎ我出来啦ᵎ我出来啦ᵎ我出来啦ᵎ', '.laytable-cell-2-cot', {
	//			time: 0
	//		});
	//	}).on('mouseleave', '.laytable-cell-2-cot', function() {
	//		layer.close(tip_index);
	//	});
	//	$('.layui-tab-content').find('.layui-table-body').find("table").find("tbody").children("tr").children("td").children(".laytable-cell-2-cot").on('mouseover', function() {
	//		var tip_indexd = layer.tips("fddd");
	//		layer.tips('我是另外一', {
	//			tips: [1, '#3595CC'],
	//			time: 1000
	//		});
	//	});

	//正在跟进
	function ta_render(curr, url) {
		var getPicTabNum = localStorage.getItem("picTabNum"); //获取缓存
		var table_where;
		if(getPicTabNum == 7) {
			table_where = {}
		} else {
			table_where = {
				'effective': curr
			};
		};
		table.render({
			elem: '#LAY_table_usering',
			size: 'sm',
			url: url,
			where: table_where,
			response: {
				dataName: 'perm',
				statusCode: 200
			},
			cols: [
				[{
					fixed: 'left',
					checkbox: true,
					width: '2%',
				}, {
					fixed: 'left',
					title: '序号',
					templet: '#zizeng',
					width: '2%',
				}, {
					fixed: 'client_point',
					title: '标注',
					templet: '#tubiao',
					width: '2%',
					event: 'setSign',
				}, {
					field: 'create_time',
					width: '8%',
					title: '资源时间',
					event: 'edit',
				}, {
					field: 'client_name',
					width: '4%',
					title: '客户姓名',
					event: 'edit',
				}, {
					field: 'client_contact',
					width: '7%',
					title: '联系方式',
					event: 'edit',
				}, {
					field: 'client_social',
					width: '6%',
					title: '微信QQ',
					event: 'edit',
				}, {
					field: 'client_address',
					width: '8.5%',
					title: '客户地址',
					templet: '<div><span title="{{d.client_address}}">{{d.client_address}}</span>/div>',
					event: 'edit',
				}, {
					field: 'client_point',
					width: '4%',
					title: '卡点',
					event: 'edit',
				}, {
					field: 'client_source',
					width: '3%',
					title: '来源',
					event: 'edit',
				}, {
					field: 'platform',
					width: '4%',
					title: '平台',
					event: 'edit',
				}, {
					field: 'client_kw',
					width: '6%',
					title: '搜索词',
					event: 'edit',
				}, {
					field: 'category',
					width: '4.4%',
					title: '属性',
					event: 'edit',
				}, {
					field: 'visit_time',
					width: '8%',
					title: '回访时间',
					event: 'edit',
				}, {
					field: 'owner_name',
					width: '3.5%',
					title: '负责人',
					event: 'edit',
				}, {
					field: 'count',
					width: '5%',
					title: '当天已接数',
				}, {
					field: 'cot',
					width: '4.8%',
					title: '当天拨打数',
				}, {
					field: 'right',
					width: '17%',
					title: '操作',
					toolbar: '#barDemo1'
				}]
			],
			page: true,
			limit: 50,
			id: 'LAY_tabled',
			done: function(res, count, curr) { //回调函数
				var code = res.code;
				if(code == 204) {
					clearCookie();
					window.parent.top.location.reload();
				};
				var data = res.data.parent;
				var data_res = res.perm;
				var perms = res.perms;
				var client_id_loca = localStorage.getItem("client_id");
				var client_id_arry = res.perm;
				$.each(client_id_arry, function(index, curr) {
					clientId.push(curr.client_id)
				});
				$("#property_seach").append(property_seach_option);
				if(getPicTabNum == 6) {
					$("#property_seach option[value=1] ").attr("selected", true)
					form.render('select')
				};

				//星星
				var lengths = data_res.length;

				$.each(data_res, function(index, curr) {
					if(curr.xg == true) {
						$("tr").eq(index + 1).css("background-color", "#c2efdf");
					}
					if(curr.lock == 1) {
						$("tr").eq(index + lengths + 2).find('.label_lock').attr({
							'lay-data': '{value:1}',
							'title': '取消关注'
						});
					}
					if(curr.client_id == client_id_loca) {
						$("tr").eq(index + 1).css("background-color", "#a4ddef")
					}
					if(curr.top == 1) {
						$("tr").eq(index + 1).find('#Stick_btn').text('取消置顶');
					}
				});

				//				var tip_index = 0;
				//				$('.layui-tab-content').find('.layui-table-body').find("table").find("tbody").children("tr").children("td").children(".laytable-cell-2-cot").on('mouseenter', function() {
				//					var id = JSON.stringify($('.layui-tab-content').find('.layui-table-body').find("table").find("tbody").find(".layui-table-hover").data('index'));
				//					var obj = data_res[id].client_comm;
				//					tip_index = layer.tips(obj, this, {
				//						time: 0,
				//						area:['400px','300px'],
				//						tips: [4],
				//					});
				//				});

				//				$(document).on('mouseenter', '.laytable-cell-2-cot', function() {
				//					var id = JSON.stringify($('.layui-tab-content').find('.layui-table-body').find("table").find("tbody").find(".layui-table-hover").data('index'));
				//					var obj = data_res[id].client_comm;
				//					tip_index = layer.tips(obj, this, {
				//						time: 0,
				//					},this);
				//				})

				//				.on('mouseleave', '.laytable-cell-2-cot', function() {
				//					layer.close(tip_index);
				//				});

				layui.each($('.label_lock'), function(index, elem) {
					var configTemp = $(elem).attr('lay-data');
					try {
						configTemp = eval('(' + configTemp + ')');
					} catch(e) {
						configTemp = {};
					}
					rate.render($.extend(true, {
						elem: elem,
						length: 1,
						readonly: false
					}, configTemp));
				})
				$.each(perms, function(index, curr) {
					switch(curr.title) {
						case "下载客户资源":
							$('.download_exl').removeClass('hide');
							break;
						case "删除客户":
							$('.dele_btn').removeClass('hide');
							break;
					}
				});

				var patent_bumemn = data.parent;
				var patent_bumen1 = patent_bumemn[0];
				principal = data.principal; //部门负责人
				var target = $("#test3");
				target.find('option').remove().end();
				target.append($("<option></option>").attr("value", '').text('--请选择--'));
				targetss.append($("<option></option>").attr("value", '').text('--请选择--'));
				$.each(patent_bumemn, function(key, role) { //遍历  
					target.append($("<option ></option>").attr("value", role).text(role));
				});
				var principal_moren = principal[0];
				$.each(principal, function(key, roles) { //遍历  
					targetss.append($("<option></option>").attr("value", roles.role_id).text(roles.name));
				});
				// form.render('select');
				var select_val_loca = localStorage.getItem("select_val1");
				var select_val_loca2 = localStorage.getItem("select_value_name");
				setTimeout(function() {
					$("#test3 option[value=" + select_val_loca + "] ").attr("selected", true);
					$("#test4 option[value=" + select_val_loca2 + "] ").attr("selected", true);
					form.render('select')
				}, 500)
				// form.render('select')
			}
		});
	};

	//监听负责人下拉框
	form.on('select(select_value_name)', function(data) {
		var select_value_name = data.value;
		localStorage.setItem("select_value_name", select_value_name); //放入session
	});

	//监听部门下拉框
	form.on('select(select_value1)', function(data) {
		var select_val1 = data.value;
		$.ajax({
			type: "get",
			beforeSend: function(request) {
				request.setRequestHeader("ApiAuth", cookie_val);
			},
			url: "http://" + com_url + "//admin/client",
			data: {
				dow: true,
				select_val: select_val1
			},
			success: function(res) {
				var principal_arry = JSON.parse(res).data.parent.principal;
				$("#test4 option").empty();
				targetss.find('option').remove().end();
				targetss.append($("<option></option>").attr("value", '').text('--请选择--'));
				$.each(principal_arry, function(key, roles) { //遍历  
					targetss.append($("<option></option>").attr("value", roles.role_id).text(roles.name));
				});
				form.render('select')
			}
		});
		localStorage.setItem("select_val1", select_val1); //放入session
	});

	//添加内容提交
	form.on('submit(demos)', function(data) {
		var ue = UE.getEditor('editorTj');
		var txt;
		ue.ready(function() {
			//				ue.setContent('hello');//设置文本编辑器
			var html = ue.getContent();
			txt = ue.getPlainTxt();
		});
		data = data.field;
		var client_name = data.client_name;
		var client_contact = data.client_contact;
		if(client_name && client_contact) {
			var index = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			$.ajax({
				type: "post",
				beforeSend: function(request) {
					request.setRequestHeader("ApiAuth", cookie_val);
				},
				data: data,
				url: "http://" + com_url + "//admin/clientAdd",
				success: function(res) {
					var code = JSON.parse(res).code;
					let msg = JSON.parse(res).msg;
					layer.close(index);
					if(code == "200") {
						$('.form_content input').val("");
						$(".layui-laypage-btn")[0].click();
						$('.add_user').css('visibility', 'hidden');
						$('.add_user .content').css('transform', 'scale(0)');
						layer.msg(msg, {
							icon: 1
						})
					} else {
						layer.msg(msg, {
							icon: 0
						})
					}
				}
			});
		} else {
			layer.msg("红色 * 号区域不能为空", {
				icon: 0
			});
		}
		return false;
	});

	//查看保存
	form.on('submit(demos2)', function(data) {
		var ueck_content = UE.getEditor('editorchak').getContent();
		if(ueck_content == "") {
			layer.msg("沟通情况不能为空!", {
				icon: 0
			})
			return false
		} else {
			data = data.field;
			$.ajax({
				type: "post",
				beforeSend: function(request) {
					request.setRequestHeader("ApiAuth", cookie_val);
				},
				data: data,
				url: "http://" + com_url + "//admin/clientSave",
				success: function(res) {
					var code = JSON.parse(res).code;
					var msg = JSON.parse(res).msg;
					if(code == "200") {
						$(".layui-laypage-btn")[0].click();
						layer.msg(msg, {
							icon: 1
						})
					} else {
						layer.msg(msg, {
							icon: 0
						})
					}
				}
			});
			return false;
		}
	});

	//编辑提交
	form.on('submit(demos3)', function(data) {
		var uebj_content = UE.getEditor('editorbianj').getContent();
		var index = layer.load(1, {
			shade: [0.5, '#fff'] //0.1透明度的白色背景
		});
		if(uebj_content == "") {
			layer.msg("沟通情况不能为空!", {
				icon: 0
			})
			layer.close(index);
		} else {
			datas = data.field;
			$('.call_phone_btn').removeAttr('disabled');
			$.ajax({
				type: "post",
				beforeSend: function(request) {
					request.setRequestHeader("ApiAuth", cookie_val);
				},
				data: datas,
				url: "http://" + com_url + "//admin/clientSave",
				success: function(res) {
					var code = JSON.parse(res).code;
					var msg = JSON.parse(res).msg;
					layer.close(index)
					if(code == "200") {
						$(".layui-laypage-btn")[0].click();
						let ue = UE.getEditor('editorbianj');
						setTimeout(function() {
							layer.msg(msg, {
								icon: 1
							})

						}, 300)
					} else {
						layer.msg(msg, {
							icon: 0
						})
					}
				}
			});
		}
		return false;
	});

	function Edit_(client_id_edit, eft) {
		var index = layer.load(1, {
			shade: [0.5, '#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type: "get",
			url: "http://" + com_url + "//admin/ClientsDetails?id=" + client_id_edit + '&effective=' + eft,
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
			},
			success: function(data) {
				localStorage.setItem("client_id", client_id_edit); //放入session
				$('#effective_statu3').find('option').remove().end();
				var Data = JSON.parse(data);
				let msg = Data.msg;
				var Data_data = Data.data;
				var change = Data_data.change;
				var code = Data.code;
				layer.close(index)
				var str = "";
				$.each(change, function(index, curr) {
					str += curr + ">"
					$('#user_historys').val(str)
				});
				var res_status = Data_data.res_status;
				var owner_role_id = Data_data.owner_role_id;
				$('#owner_role_idbj').val(owner_role_id);
				$('#owner_role_idroi').val(owner_role_id)
				var client_ids = Data_data.client_id;
				$('#client_ids').val(client_ids);
				var invalid_status = Data_data.invalid_status;
				var effective_statuss = Data_data.effective_status;
				var effective_status2 = $('#effective_status2');
				var client_comm = Data_data.client_comm;
				let ue = UE.getEditor('editorbianj');
				if(client_comm) {
					ue.ready(function() {
						ue.setContent(client_comm); //设置文本编辑器
					});
				}
				//渲染数据
				$('#owner_fixeds').val(Data_data.owner_role_id);
				$('#effective_fixeds').val(Data_data.effective_status);

				$('#owner_name').val(Data_data.owner_name);
				$('#client_names').val(Data_data.client_name);
				$('#client_socials').val(Data_data.client_social);
				$('#platform').val(Data_data.platform);
				$('#client_kws').val(Data_data.client_kw);
				$('#client_contacts').val(Data_data.client_contact);
				$('#client_addresss').val(Data_data.client_address);
				$('#client_points').val(Data_data.client_point);
				$('#clue_categoryb').val(Data_data.category);
				$('#visit_times').val(Data_data.visit_time);
				$('#platforms').val(Data_data.platform);
				$('#client_attributions').val(Data_data.client_attribution);
				if(code == "200") {
					$('.Editor-page').css('visibility', 'visible');
					$('.editor_con').css('transform', 'scale(1)');

					var effective_statu3_dome = '<option value="0">有效</option>' +
						'<option value="1">无效</option>';
					$('#effective_statu3').append(effective_statu3_dome);
					$("#effective_statu3 option[value=" + res_status + "] ").attr("selected", true);
					form.render('select');
					//判断资源状态、、具体状态
					//						if(res_status == "0") {
					effective_status2.find('option').remove().end();
					var tests = '<option value="1">跟进客户</option>' +
						'<option value="2">重点客户</option>' +
						'<option value="3">来访客户</option>' +
						'<option value="4">成交客户</option>' +
						'<option value="5">留言</option>' +
						'<option value="6">淘汰资源</option>' +
						'<option value="7">淘汰利用</option>' +
						'<option value="9">恶搞名单</option>';
					// '<option value="8">客户投诉</option>';
					effective_status2.append(tests);
					// form.render('select');
					// if(effective_statuss == effective_statuss) {
					$("#effective_status2 option[value=" + effective_statuss + "] ").attr("selected", true);
					form.render('select');
					// }
				} else {
					layer.msg(msg, {
						icon: 0
					})
				}

			},
			error: function(res) {
				alert("数据库异常");
			}
		});
	};
	//座席地区
	var zxdq = getCookie('zxhdq');
	var serverinstand;
	//七陌拨打
	function Qimo_Call(phone) {
		$.ajax({
			type: 'get',
			url: 'http:// ' + com_url + ' /moor.php',
			data: {
				FromExten: zxh,
				Exten: phone,
			},
			dataType: 'jsonp',
			jsonp: 'callblack',
			success: function(data) {
				if(data.Message == "407 Agent can not take calls") {
					alert('麻烦您登录呼叫系统再呼叫');
				}
				if(data.Message == "408 Agent Busy") {
					alert('坐席忙碌，无法接听');
				}
				if(data.Message == "500 Server error") {
					alert('服务器异常');
				}
			}
		});
	};
	//华翔拨打
	function Huax_Call(phone) {
		if(!serverinstand) {
			alert("请连接系统服务器");
		} else {
			if(phone.length == 0) {
				alert("电话号码为空");
			} else {
				serverinstand.CallPhoneByAgent(phone);
				serverinstand.CallResult = function(infoId) {
					if(infoId == 0) {
						alert("请先摘机，注：通话结束后,必须先挂机,然后再摘机");
					}
					if(infoId == -1) {
						alert("坐席" + serverinstand.agentcode + "不存在,请确认坐席号是否正确");
					}
					if(infoId == -2) {
						alert("此通道(" + serverinstand.agentcode + ")断线,无法一键拨和录音,请手拨!");
					}
				}
			}
		}
	};

	//监听工具条
	var clientId_big;
	var client_id;
	table.on('tool(demo1)', function(obj) {
		var data = obj.data;
		client_id = data.client_id;
		localStorage.setItem("client_id", client_id); //放入session
		//点击查看
		localStorage.setItem("client_id", client_id); //放入session	
		var eft = data.effective_status;
		localStorage.setItem("eft", eft); //放入session	
		if(obj.event === 'detail') { //查看
			var index = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			var client_id = data.client_id;
			localStorage.setItem("client_id", client_id); //放入session		
			let ue = UE.getEditor('editorchak');
			var index_nums = data.number;
			localStorage.setItem("index_nums", index_nums); //放入session
			var client_comm;
			$('body,html').addClass('bh_hide');
			if(data.effective_status == 8) {
				layer.msg("客户投诉电话不可拨打", {
					icon: 0,
					time: 1500
				})
				$('#view_call').attr('disabled', 'disabled');
			};
			$.ajax({
				type: "get",
				url: "http://" + com_url + "//admin/ClientsDetails?id=" + client_id,
				data: {
					all: true,
					effective: eft
				},
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
				},
				success: function(data) {
					var Data = JSON.parse(data);
					let msg = Data.msg;
					layer.close(index)
					var change = Data.data.change;
					var str = "";
					$.each(change, function(index, curr) {
						str += curr + ">"
						$('#user_history').val(str)
					});
					if(Data.code == "200") {
						$('.view_pop').css('visibility', 'visible');
						$('#view_form').css('transform', 'scale(1)');
						var Data_data = Data.data;
						var res_status = Data_data.res_status;
						var clue_owner_name = Data_data.owner_name;
						var clue_owner_role_id = Data_data.owner_role_id;
						var call_records = Data_data.call_records;
						for(var i = 0; i < call_records.length; i++) {
							var $tr = $("<tr>" +
								"<td>" + call_records[i].State + "</td>" +
								"<td>" + call_records[i].name + "</td>" +
								"<td>" + call_records[i].Begin + "</td>" +
								"<td>" + call_records[i].End + "</td>" +
								"<td>" +
								"<audio controls src='" + call_records[i].MonitorFilename + "'></audio>" +
								"</td>" +
								+"</tr>");
							$('#LAY_table_roled_son').append($tr)
						}
						$('#role_id_fz').val(clue_owner_role_id);
						$('#owner_role_idckroi').val(clue_owner_role_id);
						$('#owner_name2').val(clue_owner_name);
						var client_id = Data_data.client_id;
						$('#client_id').val(client_id);
						var invalid_status = Data_data.invalid_status;
						var effective_statuss = Data_data.effective_status;
						var effective_status = $('#effective_status');
						client_comm = Data_data.client_comm;
						if(client_comm) {
							ue.ready(function() {
								ue.setContent(client_comm); //设置文本编辑器
							});
						}

						//渲染数据

						$('#owner_fixed').val(Data_data.owner_role_id);
						$('#effective_fixed').val(Data_data.effective_status);

						$('#owner_name').val(Data_data.owner_name);
						$('#client_name').val(Data_data.client_name);
						$('#client_social').val(Data_data.client_social);
						$('#platform').val(Data_data.platform);
						$('#client_kw').val(Data_data.client_kw);
						$('#client_contact').val(Data_data.client_contact);
						$('#client_address').val(Data_data.client_address);
						$('#client_point').val(Data_data.client_point);
						$('#clue_categorys').val(Data_data.category);

						$('#visit_time').val(Data_data.visit_time);
						$('#client_attribution').val(Data_data.client_attribution);
						$('#clue_create_time').text(Data_data.clue_create_time);
						$('#clue_create_name').text(Data_data.clue_create_name);
						$('#clue_update_time').text(Data_data.clue_update_time);
						$('#clue_project').text(Data_data.clue_project);
						$('#clue_phone').text(Data_data.clue_phone);
						$('#clue_owner_name').text(Data_data.owner_name);
						$('#clue_area').text(Data_data.clue_area);
						$('#clue_wechat').text(Data_data.clue_wechat);
						$('#clue_qq').text(Data_data.clue_qq);
						$('#clue_number').text(Data_data.clue_number);
						$('#clue_unit').text(Data_data.clue_unit);
						$('#clue_platform').text(Data_data.clue_platform);
						$('#clue_client').text(Data_data.clue_client);
						$('#clue_account_name').text(Data_data.clue_account_name);
						$('#clue_coding').text(Data_data.clue_coding);
						$('#clue_source_platform').text(Data_data.clue_source_platform);
						$('#clue_category').text(Data_data.category);
						$('#clue_ip').text(Data_data.clue_ip);
						$('#clue_source').text(Data_data.clue_source);
						$('#clue_source_link').text(Data_data.clue_source_link);
						$('#clue_key').text(Data_data.clue_key);
						$('#clue_details').html(Data_data.clue_details);

						var effective_statu_dome = '<option value="0">有效</option>' +
							'<option value="1">无效</option>';
						$('#effective_statu').append(effective_statu_dome);
						$("#effective_statu option[value=" + res_status + "] ").attr("selected", true);
						form.render('select');
						var tests = '<option value="1">跟进客户</option>' +
							'<option value="2">重点客户</option>' +
							'<option value="3">来访客户</option>' +
							'<option value="4">成交客户</option>' +
							'<option value="5">留言</option>' +
							'<option value="6">淘汰资源</option>' +
							'<option value="7">淘汰利用</option>' +
							'<option value="9">恶搞名单</option>';
						// '<option value="8">客户投诉</option>';
						effective_status.append(tests);
						// form.render('select');
						// if(effective_statuss == effective_statuss) {
						$("#effective_status option[value=" + effective_statuss + "] ").attr("selected", true)
						form.render('select');
						// }
					} else {
						layer.msg(msg, {
							icon: 0
						});
					};
					clue_source_links = $('#clue_source_link').text();

				},
				error: function(res) {
					layer.msg(msg);
				}
			});
		} else if(obj.event === 'edit') { //点击编辑
			clientId_big = data.client_id;
			if(data.effective_status == 8) {
				layer.msg("客户投诉电话不可拨打", {
					icon: 0,
					time: 1500
				})
				$('.call_phone_btn').attr('disabled', 'disabled');
			};
			Edit_(client_id, eft)
		} else if(obj.event === 'call_ip') {
			//操作拨打按钮
			if(data.effective_status == 8) {
				layer.msg("客户投诉电话不可拨打", {
					icon: 0,
					time: 1500
				})
				return false;
			}
			var phone_num = data.client_contact; //手机号码
			if(data.client_contact) {
				if(zxdq == '1') {
					Qimo_Call(phone_num);
				} else {
					Huax_Call(phone_num);
				};
			} else {
				layer.msg("手机号为空，无法拨打", {
					icon: 0
				});
			}
		} else if(obj.event === 'call_ip_jl') {
			//加0呼拨打按钮
			if(data.effective_status == 8) {
				layer.msg("客户投诉电话不可拨打", {
					icon: 0,
					time: 1500
				})
				return false;
			}
			var phone_num = data.client_contact; //手机号码
			if(data.client_contact) {
				if(zxdq == '1') {
					Qimo_Call('0'+phone_num);
				} else {
					Huax_Call('0'+phone_num);
				};
			} else {
				layer.msg("手机号为空，无法拨打", {
					icon: 0
				});
			}
		} else if(obj.event === 'weed') {
			layer.open({
				type: 1,
				title: "提示",
				skin: 'add_department_content',
				area: "200px",
				content: "确认淘汰当前资源么？",
				btnAlign: 'c',
				btn: ['确定', '取消'],
				btn1: function(index, curr) {
					$.ajax({
						type: 'post',
						beforeSend: cookie_val_fun,
						url: "http://" + com_url + "//admin/clienteliminate",
						data: {
							client_id: client_id,
						},
						dataType: 'json',
						success: function(res) {
							var msg = res.msg;
							var code = res.code;
							if(code == "200") {
								$(".layui-laypage-btn")[0].click();
								layer.close(index);
								layer.msg(msg, {
									icon: 1
								});
							} else {
								layer.close(index);
								layer.msg(msg, {
									icon: 0
								});
							}
						},
						error: function(res) {
							alert("数据库异常")
						},
					});
				}
			})
		} else if(obj.event === 'setSign') {
			let client_id_lock = data.client_id;
			$.ajax({
				type: 'get',
				url: "http://" + com_url + "//admin/lock",
				beforeSend: cookie_val_fun,
				data: {
					id: client_id_lock,
				},
				success: function(res) {
					var data = JSON.parse(res);
					var code = data.code;
					var msg = data.msg;
					if(code == "200") {
						$(".layui-laypage-btn")[0].click();
						setTimeout(function() {
							layer.msg(msg, {
								icon: 1
							})
						}, 500)
					} else {
						layer.msg(msg, {
							icon: 0
						})
					}
				}
			});
		} else if(obj.event === 'stick') {
			var Top_id = data.top;
			var top_shade = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			$.ajax({
				type: "get",
				beforeSend: cookie_val_fun,
				data: {
					top: Top_id,
					id: client_id
				},
				url: "http://" + com_url + "//admin/clientTop",
				success: function(res) {
					let data = JSON.parse(res);
					let code = data.code;
					let msg = data.msg;
					if(code == 200) {
						$(".layui-laypage-btn")[0].click();
						setTimeout(function() {
							layer.msg(msg, {
								icon: 1
							})
							layer.close(top_shade);
						}, 1000)
					} else {
						layer.msg(msg, {
							icon: 0
						})
					}
				},
			});
		} else if(obj.event === 'Deles') {
			var dele_shade = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			layer.open({
				type: 1,
				title: "提示",
				skin: 'add_department_content',
				area: "200px",
				content: "确认删除么？",
				btnAlign: 'c',
				btn: ['确定', '取消'],
				btn1: function(index, curr) {
					$.ajax({
						type: "get",
						beforeSend: cookie_val_fun,
						data: {
							client_id: client_id
						},
						url: "http://" + com_url + "//admin/clientDel",
						success: function(res) {
							let data = JSON.parse(res);
							let code = data.code;
							let msg = data.msg;
							layer.close(index);
							layer.close(dele_shade);
							if(code == 200) {
								$(".layui-laypage-btn")[0].click();
								setTimeout(function() {
									layer.msg(msg, {
										icon: 1
									})
								}, 800)
							} else {
								layer.msg(msg, {
									icon: 0
								})
							}
						},
						error: function(res) {
							alert("数据库异常")
						},
					});
				},
				btn2: function() {
					layer.close(dele_shade);
				},
				cancel: function() {
					layer.close(dele_shade);
				}
			})

		}
	});

	$('#clue_source_link').css('cursor', 'pointer');
	$('#clue_source_link').click(function() {
		window.parent.open(clue_source_links);
	})

	//下一页
	var Next_num = 1;
	var Top_num = -1;
	form.on('submit(next_page)', function(data) {
		edit_empty();
		var Next_client_id = parseInt(localStorage.getItem("client_id"));
		var Next_bs = Next_num++;
		var Next_client_arr = clientId.indexOf(Next_client_id);
		var Next_loca = clientId[Next_client_arr + 1];
		//		return false
		//		localStorage.setItem("client_id", Next_loca); //放入session
		let eft_loca = localStorage.getItem("eft");
		if(Next_loca == undefined) {
			layer.msg('当前页已到达底部', {
				icon: 0
			});
			return false
		}
		Edit_(Next_loca, eft_loca)
		return false;
	});
	//上一页
	form.on('submit(top_page)', function(data) {
		edit_empty();
		var client_ids = parseInt(localStorage.getItem("client_id"));
		var numss = Top_num--;
		var client_arr = clientId.indexOf(client_ids);
		// console.log(clientId)
		var client_idr = clientId[client_arr - 1];
		let eft_loca = localStorage.getItem("eft");
		// console.log(client_idr)
		if(client_idr === undefined) {
			layer.msg('当前页已到达顶部', {
				icon: 0
			});
			return false
		}
		Edit_(client_idr, eft_loca)
		return false;
	});

	var times = 0;
	var owner_name_val;
	var moblile;
	var creadtime;
	var sourc_select;
	var uSer_Name;
	var wxqq;
	var platform_seach;
	var property_seach;
	var depr;
	var user_tab_val;
	var where_obj = {};

	//全局搜索
	function where1() {
		where_obj = {
			'owner_val': owner_name_val,
			'phone': moblile,
			'time': creadtime,
			'client_source': sourc_select,
			'name': uSer_Name,
			'social': wxqq,
			'platform': platform_seach,
			'category': property_seach,
			'depr': depr,
			'effectives': user_tab_val
		}
	};

	function where2() {
		where_obj = {
			'owner_val': owner_name_val,
			'phone': moblile,
			'time': creadtime,
			'client_source': sourc_select,
			'name': uSer_Name,
			'social': wxqq,
			'platform': platform_seach,
			'category': property_seach,
			'depr': depr,
		}
	};

	//弹出层全局搜索
	var msg_type = localStorage.getItem("msg_type");

	function reload_test() {
		where_obj = {
			'client_id': msg_type
		}
	};

	var picTabNums = localStorage.getItem("picTabNums");
	if(picTabNums == 7 && getPicTabNum == 7) {
		setTimeout(function() {
			$('.searchs').trigger('click')
		}, 100)
	}

	//搜索功能
	var $ = layui.$,
		active = {
			reload_sbm: function() {
				clientId = [];
				var indexe = layer.load(1, {
					shade: [0.5, '#fff'] //1透明度的白色背景
				});
				let getPicTabNum = localStorage.getItem('picTabNum');
				//获取搜索框里面的值
				user_tab_val = $('#user_tab_val').val();
				owner_name_val = $('#test4').val();
				moblile = $('#mobile').val();
				creadtime = $('#test6').val();
				sourc_select = $('#sourc_select').val();
				uSer_Name = $('#uSer_Name').val();
				wxqq = $('#wxqq').val();
				platform_seach = $('#platform_seach').val();
				property_seach = $('#property_seach').val();
				depr = $('#test3').val();
				if(getPicTabNum < 7) {
					where2();
				} else if(getPicTabNum == 7 && picTabNums == 7) {
					reload_test();
				} else if(getPicTabNum == 7) {
					where1(); //全局搜索
				}
				//执行重载
				table.reload('LAY_tabled', {
					page: {
						curr: 1 //重新从第 1 页开始
					},
					where: where_obj,
					done: function(res) {
						setTimeout(function() {
							localStorage.setItem("picTabNums", '0');
						}, 1000)
						layer.close(indexe);
						var datas = res.perm;
						var lengths = datas.length;
						var client_id_loca = localStorage.getItem("client_id");
						//下一页
						var client_id_arry = res.perm;
						$.each(client_id_arry, function(index, curr) {
							clientId.push(curr.client_id)
						});

						var perms = res.perms;
						$.each(perms, function(index, curr) {
							switch(curr.title) {
								case "下载客户资源":
									$('.download_exl').removeClass('hide');
									break;
								case "删除客户":
									$('.dele_btn').removeClass('hide');
									break;
							}
						});
						$.each(datas, function(index, curr) {
							if(curr.xg == true) {
								$("tr").eq(index + 1).css("background-color", "#c2efdf");
							}
							if(curr.lock == 1) {
								$("tr").eq(index + lengths + 2).find('.label_lock').attr({
									'lay-data': '{value:1}',
									'title': '取消关注'
								});
							}
							if(curr.client_id == client_id_loca) {
								$("tr").eq(index + 1).css("background-color", "#a4ddef")
							}
							if(curr.top == 1) {
								$("tr").eq(index + 1).find('#Stick_btn').text('取消置顶');
							}
						});
						layui.each($('.label_lock'), function(index, elem) {
							var configTemp = $(elem).attr('lay-data');
							try {
								configTemp = eval('(' + configTemp + ')');
							} catch(e) {
								configTemp = {};
							}
							rate.render($.extend(true, {
								elem: elem,
								length: 1,
								readonly: false
							}, configTemp));
						})
					}
				});
			},
			//编辑拨打电话
			edit_call: function() {
				var edit_phone_num = $('#client_contacts').val();
				if(edit_phone) {
					if(zxdq == '1') {
						Qimo_Call(edit_phone_num);
					} else {
						Huax_Call(edit_phone_num);
					};
				} else {
					layer.msg("手机号为空，无法拨打", {
						icon: 0
					});
				}
			},
			//加0呼编辑拨打电话
			edit_call_jl: function() {
				var edit_phone_num = $('#client_contacts').val();
				if(edit_phone) {
					if(zxdq == '1') {
						Qimo_Call('0'+edit_phone_num);
					} else {
						Huax_Call('0'+edit_phone_num);
					};
				} else {
					layer.msg("手机号为空，无法拨打", {
						icon: 0
					});
				}
			},
			//查看拨打电话
			view_call: function() {
				var view_phone = $('#client_contact').val();
				if(view_phone) {
					if(zxdq == '1') {
						Qimo_Call(view_phone);
					} else {
						Huax_Call(view_phone);
					};
				} else {
					layer.msg("手机号为空，无法拨打", {
						icon: 0
					});
				}
			},
			//加0呼查看拨打电话
			view_call_jl: function() {
				var view_phone = $('#client_contact').val();
				if(view_phone) {
					if(zxdq == '1') {
						Qimo_Call('0'+view_phone);
					} else {
						Huax_Call('0'+view_phone);
					};
				} else {
					layer.msg("手机号为空，无法拨打", {
						icon: 0
					});
				}
			},
			//批量分配
			distribution: function(obj) {
				var checkStatus = table.checkStatus('LAY_tabled'),
					data = checkStatus.data;
				var datass = JSON.parse(JSON.stringify(data));
				if(datass == "") {
					layer.msg('未勾选资源', {
						icon: 0
					});
					return false;
				};
				for(var i = 0; i < datass.length; i++) {
					var idss = datass[i].client_id;
					allcation_id.push(idss); //获取分配人的id
				};
				$('.Allcation').css('visibility', 'visible');
				$('.Allcation .content').css('transform', 'scale(1)');
			},
			login_btns: function() {
				if(zxdq == 1) {
					//七陌
					function Qimo() {
						var floor = getCookie("floor");
						var sjs = "767089366";
						if(floor == '3') {
							var url = "http://10.30.80.100/app";
							var PBX = 'btg.gzqd.1.1';
						} else if(floor == '4') {
							var url = "http://10.30.80.101/app";
							var PBX = 'btg.gzqd.2.1';
						} else {
							var url = "http://10.30.80.102/app";
							var PBX = 'btg.gzqd.3.1';
						}
						$('#logout').attr("data-sj", sjs);
						$.ajax({
							type: 'get',
							url: url,
							data: {
								Action: 'SignIn',
								ActionID: 'SignIn0.' + sjs,
								Account: 'N00000011697',
								Exten: zxh,
								PBX: PBX,
								ExtenType: 'gateway',
							},
							dataType: 'json',
							success: function(data) {
								//								console.log(data);
							}
						});
						alert('登录呼叫系统成功！可以拨打电话！');
					};
					Qimo();
				} else {
					//华翔
					function Huaxiang() {
						var surl = '10.50.50.231:8000';
						var agent = getCookie("zxh");
						if(agent.length == 'undefined') {
							alert("请输入登录坐席号");
						}
						serverinstand = HXCCWS.Creater({
							weburl: surl,
							agentcode: agent
						});
						serverinstand.ConnectSuccess = function() {
							alert("已成功连接呼叫系统");

						}
						serverinstand.Close = function() {
							alert("与呼叫系统连接中断");
						}
						serverinstand.Error = function() {
							alert("WebSocket连接发生错误");
						}
						serverinstand.Connect();
					};
					Huaxiang();
				};

			},
			pricipal_seach: function() {
				//执行重载
				table.reload('LAY_tabled', {
					where: {
						depars: select_val,
						owner_val: '',
						phone: '',
						time: ''
					}
				});
			},
			folwing_up: function() { //正在跟进
				ta_render(1, url_table)
			},
			del_resource: function() { //淘汰资源
				$("#test4 option").empty();
				targetss.find('option').remove().end();
				times = 1;
				ta_render(6, url_table)
			},
			take_customers: function() { //淘汰利用
				$("#test4 option").empty();
				targetss.find('option').remove().end();
				ta_render(7, url_table)
			},
			key_customers: function() { //重点客户
				targetss.find('option').remove().end();
				ta_render(2, url_table)
			},
			visit_customers: function() { //来访客户
				targetss.find('option').remove().end();
				ta_render(3, url_table)
			},
			a_customers: function() { //成交客户
				targetss.find('option').remove().end();
				ta_render(4, url_table)
			},
			batch_customers: function() { //留言
				targetss.find('option').remove().end();
				ta_render(5, url_table)
			},
			global_customers: function() { //全局搜索
				targetss.find('option').remove().end();
				ta_render("", url_table)
			},
			egao_customers: function() { //恶搞名单
				targetss.find('option').remove().end();
				ta_render(9, url_table)
			},
		};

	//监听行单击事件（单击事件为：rowDouble）
	table.on('row(demo1)', function(obj) {
		layer.alert(JSON.stringify(data), {
			title: '当前行数据：'
		});
		//标注选中样式
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
	});

	$('#batch_customers').on('click', function() {
		$('.source_msg').removeClass('hide');
		$('.user_tab').addClass('hide');
	});

	$('#global_customers').click(function() {
		$('.user_tab').removeClass('hide');
		$('.source_msg').removeClass('hide');
	})

	$('.tab_com').on('click', function() {
		$('.source_msg').addClass('hide');
		$('.user_tab').addClass('hide');
	});

	$('.layui-tab-title li').on('click', function() {
		$("#property_seach option").empty();
	});

	//批量分配
	$('.searc,#mass_distribution,#login_btn,.call_phone_btn,#view_call,.layui-tab-title li').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	$('.searchs').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
		setTimeout(function() {
			$("#test4 option").each(function() {
				text = $(this).text();
				if($("#test4 option:contains(" + text + ")").length > 1)
					$("#test4 option:contains(" + text + "):gt(0)").remove();
				form.render('select');
			});
		}, 800)
	});

	if(getPicTabNum) {
		$('.layui-tab-title li').eq(getPicTabNum).trigger('click');
	}
	var user_names;
	table.on('tool(leader)', function(obj) {
		var data = obj.data;
		var datas = JSON.parse(JSON.stringify(data))
		if(obj.event === 'setSign') {
			role_id = datas.role_id;
			var position_name = datas.position_name;
			user_names = datas.name;
		}
	});
	//点击线索负责人  ok提交按钮
	$('.OkBtn').click(function() {
		$('#owner_name2').val(user_names);
		$('#owner_role_idroi').val(role_id);
		$('#owner_role_idtj').val(role_id);
		$('#owner_role_idckroi').val(role_id);
		$('#owner_name1').val(user_names);
		$('#owner_name').val(user_names);
		$('#owner_nameadmin').val(user_names);
		if($('#owner_nameadmin').val()) {
			$('.Trace_leader').css('visibility', 'hidden');
			$('.Trace_leader .content').css('transform', 'scale(0)');
		} else {
			layer.msg("未选择联系人", {
				icon: 0
			})
		}
	});
	//ok提交
	$('#submit_id').on('click', function() {
		var role_id_arry = [];
		role_id_arry.push(role_id);
		var unime_allcation = $.unique(allcation_id);
		var owner_nameadmin = $('#owner_nameadmin').val()
		if(owner_nameadmin == "") {
			layer.msg("未选择联系人", {
				icon: 0
			})
			return false;
		}
		let getPicTabNum = localStorage.getItem('picTabNum');
		if(getPicTabNum == 1) {
			$.ajax({
				type: "post",
				url: "http://" + com_url + "//admin/clientreceive",
				data: {
					assigner: role_id,
					client_id: unime_allcation,
					week_out: true
				},
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
				},
				success: function(data) {
					var datass = JSON.parse(data);
					var code = datass.code;
					var msg = datass.msg;
					var demoReload = $('#demoReload');
					if(code == "200") {
						allcation_id = [];
						unime_allcation = [];
						role_id_arry = [];
						$('#owner_nameadmin').val("");
						$('.Allcation').css('visibility', 'hidden');
						$('.Allcation .content').css('transform', 'scale(0)');
						$(".layui-laypage-btn")[0].click();
						layer.msg(msg, {
							icon: 1
						})
					} else {
						clue_id_Array = [];
						layer.msg(msg, {
							icon: 0,
							time: 1500
						});
					}
				},
				error: function(res) {
					alert("领取失败");
				}
			});
		} else {
			$.ajax({
				type: "post",
				url: "http://" + com_url + "//admin/clientreceive",
				data: {
					assigner: role_id, //选择人的id
					client_id: unime_allcation, //选中行的id   数组形式传送
				},
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
				},
				success: function(data) {
					var datass = JSON.parse(data);
					var code = datass.code;
					let msg = datass.msg;
					var demoReload = $('#demoReload');
					if(code == "200") {
						allcation_id = [];
						unime_allcation = [];
						role_id_arry = [];
						$('#owner_nameadmin').val("");
						$('.Allcation').css('visibility', 'hidden');
						$('.Allcation .content').css('transform', 'scale(0)');
						$(".layui-laypage-btn")[0].click();
						layer.msg(msg, {
							icon: 1
						})
					} else {
						clue_id_Array = [];
						layer.msg(msg, {
							icon: 0,
							time: 1500
						});
					}
				},
				error: function(res) {
					alert("领取失败");
				}
			});

		}
	});
});